﻿using System;
using System.Collections.Generic;
using System.Text;
using TcpLabCommon;

namespace TCPIPControl
{
    public static class Cls_GetDataPacket
    {
      

        public static List<ACQ_DB> GetPacketList(string sql, bool conType, string DataSource, string InitialCatalog, string UserID, string Password)
        {
            string datasource = "";
            if (conType)
            {
                datasource = "Data Source=" + DataSource +
                 ";Initial Catalog=" + InitialCatalog +
                 ";Persist Security Info=True;User ID=" + UserID +
                 ";Password=" + Password + "";
            }
            else
            {
                datasource = "Data Source=" + DataSource +
                          ";Initial Catalog=" + InitialCatalog +
                          ";Integrated Security=True";
            }
            List<ACQ_DB> acqlist = new List<ACQ_DB>();
            MyDB_VB mydb = new MyDB_VB(datasource);
            using (mydb)
            {
                try
                {

                    mydb.Open();
                  
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        ACQ_DB acq = new ACQ_DB();
                        acq.Id = Convert.ToInt64(mydb.DataReader["Id"]);
                        acq.acqId = mydb.DataReader["acqId"].ToString();
                        acq.acqName = mydb.DataReader["acqName"].ToString();

                        byte[] by1 = mydb.DataReader["dataPaket"] as byte[];
                        //CurrentDataPacketAndFFTData dpk = Activator.CreateInstance<StructHelper>().DeSerialize<CurrentDataPacketAndFFTData>(by1);
                        //acq.dataPaket = dpk;
                        acq.insertTime = Convert.ToDateTime(mydb.DataReader["insertTime"]);
                        acq.projectId = Convert.ToInt32(mydb.DataReader["projectId"]);

                   
                        acqlist.Add(acq);
                    }
                    mydb.Close();
                }
                catch (Exception)
                {
                    acqlist = new List<ACQ_DB>();
                }
            }
            return acqlist;
        }

        public static byteInt[] GetInitialdata(int channelid, CurrentDataPacketAndFFTData a)
        {
            return a.CurrentdataPacket.InitialData[channelid].Data;
        }



    }


    public class ACQ_DB
    {
        public long Id;
        public string acqId;
        public string acqName;
        public int testPointId;
        public CurrentDataPacketAndFFTData dataPaket;
        public DateTime insertTime;
        public int userId;
        public int projectId;
        public int testItemId;
        public int taskId;
        public int State;

    }

}

